<template>
  <div>
    <!-- 搜索 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="110" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable ref="table" :columns="columns" :params="fetchParams" :fetchapi="fetchapi" :datakey="'records'" :onColumnsChange="(columns) => (this.columns = columns)" :isSelectColumn="false">
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" icon="el-icon-plus" :click="exportFileBtn.bind()">导出</multiuse-button>
      </template>
    </FilterTable>
    <!-- <BaseDialog :visible.sync="visibleAdd" :title="'旧件销毁申请'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <AddForm @close="closeAdd" @save="saveAdd"></AddForm>
    </BaseDialog>-->
    <Drawer :visible.sync="visibleEdit" :title="'旧件销毁结果编辑'" destroyOnClose width="70%" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <EditForm @close="closeEdit" @save="saveEdit" :editRow="editRow"></EditForm>
    </Drawer>
  </div>
</template>
<script>
import EditForm from './EditForm.vue';
import moment from "moment";

import { getListByPage, edit, excelExport,exporturl } from '@/api/usedPartsDestructionDealer/DestructionResultsUpload';
import { getPartType } from '@/api/dictionary';
import { exportExcelFile } from '@/utils/index';
import Dealer from '../../../projectOperating/program/commonForm/dealer';
import { getAppId } from '@/assets/js/auth';

export default {
  components: {
    Dealer,
    EditForm,
  },
  data() {
    return {
      dataList: [],
      visible: false,
      visibleDrawer: false,
      collapse: true,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      // fetchApi: getUser,
      fetchParams: {
        statusCodes: '55311003,55311006,55311009',
        xhrAbort: true
      }, //ipa 接口
      selectedRows: [], //选中经销商
      dialogTitle: '选择车辆',
      visibleAdd: false,
      fetchapi: getListByPage,
      editRow: {},
      visibleEdit: false,
    };
  },
  created() {
    this.fetchParams.beginReportingTime = moment().startOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.fetchParams.endReportingTime = moment().endOf('week').format('YYYY-MM-DD HH:mm:ss');
  },
  methods: {
    async deleteHandle(row) {
      let res = await edit(row.destoryId);
      this.editRow = res.data;
      this.visibleEdit = true;
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    close() {
      this.visible = false;
    },
    chooseOrderNo() {
      this.visible = true;
    },
    save(obj) {
      this.topFilterInit.find((x) => x.fieldName == 'model').initialValue = obj[0].address;
      this.visible = false;
    },
    add() {
      this.visibleAdd = true;
    },
    closeEdit() {
      this.visibleEdit = false;
    },
    saveEdit() {
      this.visibleEdit = false;
      this.fetchParams = { ...this.fetchParams };
    },
    closeAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    saveAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //搜索获取数据
    changeHandle(val) {
      if(!val.beginReportingTime) {
        this.$notify.warning({ title: '提示信息', message: '提报开始日期不可为空！' });
        return
      }
      if(!val.endReportingTime) {
        this.$notify.warning({ title: '提示信息', message: '提报结束日期不可为空！' });
        return
      }
      if(getAppId() == 'arcfox') {
        if (moment(moment(val.endReportingTime).format('YYYY-MM-DD')).diff(moment(val.beginReportingTime), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '提报日期跨度不可超过十二个月！' });
          return
        }
      } else {
        if (moment(moment(val.endReportingTime).format('YYYY-MM-DD')).diff(moment(val.beginReportingTime), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '提报日期跨度不可超过十二个月！' });
          return
        }
      }
      this.fetchParams = Object.assign({}, this.fetchParams, val, { statusCodes: '55311003,55311006,55311009',xhrAbort: false });
    },
    // 导出
    async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000215' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // let beginSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginReportingTime|endReportingTime").initialValue[0];
      // let endSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginReportingTime|endReportingTime").initialValue[1];
      // if(!beginSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '提报开始日期不可为空！' });
      //   return
      // }
      // if(!endSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '提报结束日期不可为空！' });
      //   return
      // }
      // if(getAppId() == 'arcfox') {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '提报日期跨度不可超过十二个月！' });
      //     return
      //   }
      // } else {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '提报日期跨度不可超过十二个月！' });
      //     return
      //   }
      // }
      // let obj = Object.assign({}, this.fetchParams, { statusCodes: '55311003,55311006,55311009',xhrAbort: false });
      // obj.beginReportingTime = this.topFilterInit.find(x => x.fieldName == "beginReportingTime|endReportingTime").initialValue[0];
      // obj.endReportingTime = this.topFilterInit.find(x => x.fieldName == "beginReportingTime|endReportingTime").initialValue[1];
      // excelExport(obj).then(res => {
      //   exportExcelFile(res.data, '旧件销毁结果信息');
      // });
    },
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: '销毁单号', fieldName: 'destoryNo', placeholder: '请输入销毁单号...', initialValue: '' },
        { type: 'INPUT', label: '旧件代码', fieldName: 'partCode', placeholder: '请输入旧件代码...', initialValue: '' },
        { type: 'INPUT', label: '旧件名称', fieldName: 'partName', placeholder: '请输入旧件名称...', initialValue: '' },
        { type: 'RANGE_DATE', label: '提报时间', fieldName: 'beginReportingTime|endReportingTime', placeholder: '请输入提报时间...', initialValue: [
            moment()
              .startOf('week')
              .format('YYYY-MM-DD HH:mm:ss'),
            moment()
              .endOf('week')
              .format('YYYY-MM-DD HH:mm:ss')
          ],
          rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
        },
        { type: 'RANGE_DATE', label: '销毁日期', fieldName: 'beginDestoryAt|endDestoryAt', placeholder: '请输入销毁日期...', initialValue: '' },
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          fixed: 'left',
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 80,
          render: (props) => {
            return (
              <div style="text-align:center">
                <multiuse-button size="mini" type="text" onClick={() => this.deleteHandle(props.row)}>
                  编辑
                </multiuse-button>
              </div>
            );
          },
        },
        { title: '销毁结果状态', dataIndex: 'statusStr', sorter: true },
        { title: '销毁单号', dataIndex: 'destoryNo', sorter: true },
        { title: '索赔单数', dataIndex: 'claimNum', sorter: true },
        { title: '销毁数量', dataIndex: 'partNum', sorter: true },
        { title: '销毁日期', dataIndex: 'destoryDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
        { title: '提报时间', dataIndex: 'reportingTime', sorter: true },
        { title: '创建时间', dataIndex: 'createdAt', sorter: true },
        { title: '创建人', dataIndex: 'createdByStr', sorter: true },
      ];
    },
  },
};
</script>

<style lang="less" scoped></style>
